/**
 * Copyright (c) 2021 Choice, Inc.
 * All Rights Reserved.
 * Choice Proprietary and Confidential.
 */
package cn.ruleengine.web.mq;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import cn.hutool.core.date.DateUtil;
import cn.ruleengine.core.RuleEngineConfiguration;
import cn.ruleengine.web.config.EngineConfig;
import lombok.extern.slf4j.Slf4j;

import static cn.ruleengine.web.config.rabbit.RabbitTopicConfig.FANOUT_EXCHANGE;

@Slf4j
@Component
public class RuleSetChangeConsumer {

    @Resource
    private EngineConfig            engineConfig;
    @Resource
    private RuleEngineConfiguration ruleEngineConfiguration;

    @RabbitListener(
            bindings = @QueueBinding(value = @Queue(),
                    exchange = @Exchange(value = FANOUT_EXCHANGE,type = ExchangeTypes.FANOUT))
    )
    public void handler(Map<String, Object> object) {
        log.info("[MQ] RuleSetChangeConsumer.handler begin:{}",
                DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        engineConfig.refresh(ruleEngineConfiguration);
        log.info("object={}", object);
    }

}
